package com.portal.framework.page;

import java.io.Serializable;
import java.util.List;

import com.portal.framework.dao.BaseIDAO;
import com.portal.framework.dao.ISearchForm;
import com.portal.framework.page.pagebar.IPageBarFormatter;
import com.portal.framework.page.pagebar.PageBarFormatterFactory;

/**
 * 功能列表:<BR>
 * <LI>能在HTML的FORM中添加所有搜索条件的hidden属性
 * <LI>支持输出上一页,下一页等的分页栏.用支持多种样式输出（需要定义接口）
 * <LI>保存BaseIDAO.得到PagerList就可以根据WHERE语句得到结果集合
 * <LI>包含搜索的结果数据,也可以得到不分页的结果.
 * <LI>只支持hibernate和spring
 * 
 * @author 卢烁波
 * @date 2007-3-10
 */
public class PagerList<T> {
	// 默认的分页大小
	public static final int DEFAULT_PAGE_SIZE = 50;

	// --------- 分页信息-----------
	// 总的记录数
	private int total;
	// 总的页数
	private int totalPage;
	// 每页记录数
	private int pageSize;
	// 当前页数
	private int curPage;

	// ------------- 结果信息 -----------
	private List<T> result;// 结果列表

	// ------------ 页面显示信息 --------
	private String pageBar;// 分页条
	private IPageBarFormatter pagerBarFomatter; // 分页条的格式化

	// ----------- 得到搜索条件------------
	private String hiddenFields;// 搜索表单的字段，用于要页面中生成,包括page,pageSize等信息
	private String pageForm;// 包括hiddenFields，FORM表单，和分页的JS代码

	private ISearchForm searchForm;// 保存搜索表单，可以根据搜索表单到WHERE条件

	// 进行搜索的模块的DAO
	private BaseIDAO<?, ? extends Serializable> baseDAO;

	public PagerList() {
	}

	public PagerList(int total, int pageSize, int curPage) {
		create(total, pageSize, curPage);
	}

	public PagerList(int total, int pageSize, int curPage,
			ISearchForm searchForm) {
		create(total, pageSize, curPage);
		this.searchForm = searchForm;
	}

	public IPageBarFormatter getPagerBarFomatter() {
		return pagerBarFomatter;
	}

	public void setPagerBarFomatter(IPageBarFormatter pagerBarFomatter) {
		this.pagerBarFomatter = pagerBarFomatter;
	}

	public String getPageBar() {
		return pageBar;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	private void create(int total, int pageSize, int curPage) {
		this.total = total;
		totalPage = 0;
		this.pageSize = pageSize;
		this.curPage = curPage;
		count();
	}

	private void count() {
		if (pageSize <= 0)
			pageSize = DEFAULT_PAGE_SIZE;
		totalPage = total / pageSize;
		if (total % pageSize > 0)
			totalPage++;
		if (curPage <= 0)
			curPage = 1;
		if (curPage > totalPage)
			curPage = totalPage;
	}

	public int getTotal() {
		return total;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
		count();
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
		count();
	}

	public void setTotal(int total) {
		this.total = total;
		count();
	}

	/**
	 * 得到要查找第一条数据的index
	 * 
	 * @return
	 */
	public int getStart() {
		return (curPage > 1) ? (curPage - 1) * pageSize : 0;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPage() {
		return curPage + 1 <= totalPage ? curPage + 1 : totalPage;
	}

	/**
	 * 前一页
	 * 
	 * @return
	 */
	public int getPrevPage() {
		return curPage == 1 ? 1 : curPage - 1;
	}

	/**
	 * 得到根据条件搜索的结果列表，但结果不分页。可以理解成每页显示数为无限大
	 * 
	 * @return
	 */
	public List<T> getAllResult() {
		return null;
	}

	public BaseIDAO<?, ? extends Serializable> getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseIDAO<?, ? extends Serializable> baseDAO) {
		this.baseDAO = baseDAO;
	}

	public String getHiddenFields() {
		return hiddenFields;
	}

	public void setHiddenFields(String hiddenFields) {
		this.hiddenFields = hiddenFields;
	}

	public ISearchForm getSearchForm() {
		return searchForm;
	}

	public void setSearchForm(ISearchForm searchForm) {
		this.searchForm = searchForm;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getPageForm() {
		return pageForm;
	}

	/**
	 * 计算
	 */
	public void compute() {
		if (this.getPagerBarFomatter() == null) {
			setPagerBarFomatter(PageBarFormatterFactory.getDefault());
		}

		PagerList<? extends Object> formatter = getPagerBarFomatter()
				.getPageBar(this);

		this.pageForm = formatter.getPageForm(); // 包括hiddenFields，FORM表单，和分页所需的JS代码
		this.hiddenFields = formatter.getHiddenFields(); // hiddenFields
		this.pageBar = formatter.getPageBar();// 分页条
	}

	public void setPageBar(String pageBar) {
		this.pageBar = pageBar;
	}

	public void setPageForm(String pageForm) {
		this.pageForm = pageForm;
	}

}
